package com.it.gmall.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.it.gmall.common.constant.RedisConst;
import com.it.gmall.common.util.MD5;
import com.it.gmall.model.user.UserAddress;
import com.it.gmall.model.user.UserInfo;
import com.it.gmall.user.mapper.UserAddressMapper;
import com.it.gmall.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    // 用户登录
    @Override
    public UserInfo userLogin(UserInfo userInfo) {
        // 1.从db查询
        UserInfo info = userMapper.selectOne(new QueryWrapper<UserInfo>()
                .eq("login_name", userInfo.getLoginName())
                .eq("passwd", MD5.encrypt(userInfo.getPasswd())));

        return info;
    }

    // 获取用户的地址信息
    @Override
    public List<UserAddress> getUserAddress(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        if(!StringUtils.isEmpty(userId)){
            List<UserAddress> addressList = userAddressMapper.selectList(new QueryWrapper<UserAddress>().eq("user_id", userId));
            return addressList;
        }
        return null;
    }
}
